using System;
using System.Net.Sockets;
using System.Text;

namespace RxjhServer.bbg
{
	public class ClientConnection : SockClienT
	{
		public ClientConnection(Socket sock, RemoveClientDelegateE rcd)
			: base(sock, rcd)
		{
		}

		public override void ProcessDataReceived(byte[] data, int length)
		{
			int num = 0;
			if (170 != data[0] || 136 != data[1])
			{
				return;
			}
			byte[] array = new byte[4];
			Buffer.BlockCopy(data, 2, array, 0, 4);
			int num2 = BitConverter.ToInt32(array, 0);
			if (length < num2 + 6)
			{
				return;
			}
			while (true)
			{
				if (World.jlMsg == 1)
				{
					Form1.WriteLine(1, "ProcessDataReceived");
				}
				byte[] array2 = new byte[num2];
				Buffer.BlockCopy(data, num + 6, array2, 0, num2);
				num += num2 + 6;
				DataReceived(array2, num2);
				if (num >= length || data[num] != 170 || data[num + 1] != 136)
				{
					break;
				}
				Buffer.BlockCopy(data, num + 2, array, 0, 4);
				num2 = BitConverter.ToInt16(array, 0);
			}
		}

		public void DataReceived(byte[] data, int length)
		{
			if (World.jlMsg == 1)
			{
				Form1.WriteLine(1, "ProcessDataReceived()");
			}
			try
			{
				byte[] array = new byte[length];
				for (int i = 0; i < length; i++)
				{
					array[i] = data[i];
				}
				string @string = Encoding.Default.GetString(array);
				string text = "-1";
				string[] array2 = @string.Split(',');
				switch (array2[0])
				{
				case "1用户登陆1":
					text = ((World.检查玩家(array2[1]) != null) ? "登陆成功" : "登陆失败");
					break;
				case "2查询2":
				{
					Players players2 = World.检查玩家(array2[1]);
					if (players2 == null)
					{
						text = "-1";
					}
					else
					{
						players2.查百宝阁元宝数();
						text = players2.FLD_RXPIONT.ToString() + "," + players2.FLD_RXPIONTX.ToString() + "," + players2.得到包裹空位数().ToString();
					}
					break;
				}
				case "3购买3":
				{
					Players players3 = World.检查玩家(array2[1]);
					text = ((players3 == null) ? "-1" : ((int.Parse(array2[5]) != 5) ? players3.百宝阁买卖东西(int.Parse(array2[2]), int.Parse(array2[3]), int.Parse(array2[4])) : players3.百宝阁买卖东西(int.Parse(array2[2]), int.Parse(array2[3]), int.Parse(array2[4]))));
					break;
				}
				case "获取角色名":
				{
					string text2 = array2[1];
					Players players = World.检查玩家(array2[1]);
					text = ((players != null) ? players.UserName : "-1");
					break;
				}
				default:
					text = "-1";
					break;
				}
				Sendd(text);
			}
			catch (Exception)
			{
				Dispose();
			}
		}
	}
}
